﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 剪切命令。
    /// ★①，修改类名。
    /// </summary>
    public static class CutCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static CutCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand("CutCommand", "CutCommand",
                typeof(CutCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.X,ModifierKeys.Control,"Ctrl+X")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。
            if (Globals.MainWindow.SelectedTitlesList.Contains(Globals.MainWindow.RootTitle))
            {
                MessageBox.Show("根标题不允许进行[剪切、复制、粘贴、删除]等操作。",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Cut();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static void Cut()
        {
            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                DeleteCommand.DeleteTitleImage(true);
                return;
            }

            if (Globals.MainWindow.SelectedComment != null)
            {
                DeleteCommand.DeleteComment(true);
                return;
            }

            if (Globals.MainWindow.SelectedPictureBox != null)
            {
                DeleteCommand.DeletePictureBox(true);
                return;
            }

            if (Globals.MainWindow.SelectedRelation != null)
            {
                DeleteCommand.DeleteRelation(true);
                return;
            }
            
            DeleteCommand.DeleteTitle(true);
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。

            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedRelation != null)
            {
                e.CanExecute = true; return;//关系线于2011年12月3日支持剪切/复制。
            }

            if (Globals.MainWindow.SelectedComment != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedPictureBox != null)
            {
                e.CanExecute = true; return;
            }
            
            if (Globals.MainWindow.SelectedTitlesList.Count <= 0)
            {
                e.CanExecute = false;
            }
            else
            {
                if (Globals.MainWindow.SelectedTitlesList.Contains(Globals.MainWindow.RootTitle))
                {
                    e.CanExecute = false;//根标题不允许剪切、复制、粘贴、删除。
                    return;
                }

                //检查是否同一阶
                if (Globals.MainWindow.SelectedTitlesList.Count == 1)
                {
                    e.CanExecute = true;
                }
                else
                {
                    Title fstTitle = Globals.MainWindow.SelectedTitlesList[0];

                    bool sameHigerID = true;

                    for (int i = 1; i < Globals.MainWindow.SelectedTitlesList.Count; i++)
                    {
                        Title t = Globals.MainWindow.SelectedTitlesList[i];
                        if (t.HigherLevelTitleID != fstTitle.HigherLevelTitleID)
                        {
                            sameHigerID = false; break;
                        }
                    }

                    e.CanExecute = sameHigerID;//只允许同阶剪切。
                }
            }
        }
    }
}
